package com.ebuc.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import com.ebuc.R

/**
 * TODO: document your custom view class.
 */
class Cube : View {

    lateinit var bitmap: Bitmap
    lateinit var painter: Paint

    /**
     * In the example view, this drawable is drawn above the text.
     */
    var exampleDrawable: Drawable? = null

    constructor(context: Context) : super(context) {
        init(null, 0)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(attrs, 0)
    }

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
        init(attrs, defStyle)
    }

    private fun init(attrs: AttributeSet?, defStyle: Int) {
        // Load attributes
        val a = context.obtainStyledAttributes(
            attrs, R.styleable.Cube, defStyle, 0
        )
        bitmap = BitmapFactory.decodeResource(resources, R.drawable.eg_image)
        a.recycle()
        painter = Paint(Paint.ANTI_ALIAS_FLAG)

    }


    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val bitmapWidth = bitmap.getScaledWidth(canvas)
        val bitmapHeight = bitmap.getScaledHeight(canvas)
        val left = (measuredWidth - bitmapWidth) / 2
        val top = (measuredHeight - bitmapHeight) / 2
        canvas.drawBitmap(
            bitmap,
            Rect(0, 0, 0 + bitmapWidth, 0 + bitmapHeight),
            RectF(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat()),
            painter
        )
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()

    }
}
